#!/usr/bin/env python3

import os
import sys
import time
import threading

class Term:
    def __init__(self):
        self.w, self.h = os.get_terminal_size()
        os.system("stty -echo -icanon")

    def __del__(self):
        os.system("stty echo icanon")

    def get(self, num=1):
        return sys.stdin.read(num)

    @property
    def size(self):
        self.w, self.h = os.get_terminal_size()
        return self.w, self.h

    def sleep(self, sec=0.1):
        time.sleep(sec)
        return self

    @staticmethod
    def run(call, interval=None, args=None, kwargs=None):
        if interval is None:
            t = threading.Thread(target=call,
                                    args=args, kwargs=kwargs, daemon=True)
        else:
            t = threading.Timer(interval, call, args=args, kwargs=kwargs)
        t.start()
        return t


if __name__ == '__main__':
    g = Term()

    print(g.size, g.w, g.h)

    def test(data):
        print("test data: ", data)
        g.sleep(5)
        print("test end")

    t = g.run(test, args=("hello", ))

    while True:
        ch = g.get()
        if ch == 'q':
            break

